package com.iotek.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.iotek.entity.Customer;
import com.iotek.entity.Detail;
import com.iotek.entity.Goods;
import com.iotek.entity.Order;
import com.iotek.service.IDetailService;
import com.iotek.service.IOrderService;
import net.sf.json.JSONArray;

@Controller
public class DetailController {
	// 声明一个订单服务对象
	@Autowired
	private IDetailService iDetailService;
	
	//声明一个详细订单对象
	@Autowired
	private IOrderService iOrderService; 
	
	/**
	 * 根据登录id查询订单信息
	 */
	@RequestMapping("insertDetail.action")
	public void insertDetail() {
		
	}
	
	/**
	 * 根据登录id查询订单信息
	 */
	@RequestMapping("getData.action")
	public void getData(HttpServletResponse httpServletResponse) {
		// 设置response的编码格式
		httpServletResponse.setCharacterEncoding("UTF-8");
		httpServletResponse.setContentType("UTF-8");
		// 使用订单服务对象获取订单数据
		List<Detail> detailsList = iDetailService.selectAllDetails(4);
		// 把对象变为JSON字符串
		String detailJson = JSON.toJSONString(detailsList);
		// 使用打印流输出内容
		try {
			httpServletResponse.getWriter().write(detailJson);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("postData.action")
	public String postData(HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
		// 设置request的编码格式
		String goodsJaon = httpServletRequest.getParameter("goodsContent");
		// 解析前台的字符串
		JSONArray json = JSONArray.fromObject(goodsJaon);
		// 使用JSON把字符串格式变为对象
		List<Detail> goodsList = (List<Detail>) JSONArray.toCollection(json, Detail.class);

		// 设置request的编码格式
		String idContent = httpServletRequest.getParameter("idContent");
		// 解析前台的字符串
		JSONArray jsonId = JSONArray.fromObject(idContent);
		// 使用JSON把字符串格式变为对象
		List<Integer> idList = (List<Integer>) JSONArray.toCollection(jsonId, Integer.class);
		
		//创建一个空的订单id用来保存需要购买的商品订单
		List<Detail> payList = new ArrayList<Detail>();
		//外层循环购物车中所有商品
		for(int i = 0;i < goodsList.size();i ++) {
			//内层循环选中的订单id
			for(int j = 0;j < idList.size();j ++) {
				//声明两个变量
				Integer detailId = goodsList.get(i).getDetailId();
				Integer id = idList.get(j);
				if(detailId == id){
					payList.add(goodsList.get(i));
				}
			}
		}
		
		//创建一个变量计算总价
		Integer total = 0;
		//循环计算总价
		for(int i = 0; i < payList.size();i ++) {
			//取出数量
			Integer number = Integer.valueOf(payList.get(i).getBuyNumber());
			//取出总价
			Integer money = Integer.valueOf( payList.get(i).getGoodsPrice());
			//计算总价
			total +=  number * money;
		}
		//把金额转换为字符串类型
		String totalMoney = String.valueOf(total);
		
		//创建一个空列表存储详细订单表
		List<Order> orderList = new ArrayList<Order>();
		//对payList循环创建添加详细订单表
		for(int i = 0; i < payList.size();i ++) {
			//获取当前时间
			long time = System.currentTimeMillis();
			//时间转换为字符串格式
			String orderNumber = String.valueOf(time);
			//new日期对象
			Date date = new Date(time);
			//转换提日期输出格式
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			//创建一个order对象
			Order order = new Order();
			//为order添加内容
			order.setOrderId(payList.get(i).getDetailId());//设置详细订单的id
			order.setDetailsId(payList.get(i).getDetailId());//添加订单的编号
			order.setCustomerId(payList.get(i).getCustomerId());//添加顾客的编号
			order.setOrderTotal(totalMoney);//设置金额
			order.setOrderNumber(orderNumber);//设置订单编号
			order.setCreatTime(dateFormat.format(date));//设置订单日期
			order.setOrderState("未支付");
			//把对象添加到支付列表中
			orderList.add(i, order);
		}

		//循环插入数据库
		for (Order order : orderList) {
			iOrderService.insertOrder(order);
		}
		
		//返回支付controller
		return "payment/paymentA.action";
	}
	
	/**
	 * 添加detail
	 * @param httpServletResponse
	 * @return
	 */
	@RequestMapping("insertCart.action")
	public void insertCart(Goods goods, HttpSession httpSession) {
		//使用session获取顾客对象
		Customer customer = (Customer) httpSession.getAttribute("customer");
		//创建一个订单对象
		Detail detail = new Detail();
		//设置相应的内容
		detail.setBuyNumber(goods.getNumber());
		detail.setCustomerId(customer.getCustomerId());
		detail.setGoodsId(goods.getGoodsId());
		detail.setGoodsIntroduce(goods.getGoodsIntroduce());
		detail.setGoodsName(goods.getGoodsName());
		detail.setGoodsPrice(goods.getGoodsPrice());
		detail.setGoodsImg(goods.getGoodsImg());
		//使用数据库语句进行添加
		iDetailService.insertDetail(detail);
	}
	

	/**
	 * 跳转到购物车
	 * 
	 * @param httpServletResponse
	 * @return
	 */
	@RequestMapping("getCart.action")
	public String getCart(HttpServletResponse httpServletResponse) {
		return "cart";
	}
}
